<?php
function proxy($url)
{
    if (!$url) {
        // Passed url not specified.
      $contents = 'ERROR: url not specified';
        $status = array('http_code' => 'ERROR');
    } else {
        $ch = curl_init($url);

        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
        }

        $cookie = array();
        foreach ($_COOKIE as $key => $value) {
            $cookie[] = $key.'='.$value;
        }
        $cookie = implode('; ', $cookie);

        curl_setopt($ch, CURLOPT_COOKIE, $cookie);

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

        list($header, $contents) = preg_split('/([\r\n][\r\n])\\1/', curl_exec($ch), 2);

        $status = curl_getinfo($ch);
        curl_close($ch);
    }

    // Split header text into an array.
    $header_text = preg_split('/[\r\n]+/', $header);

	// Propagate headers to response.
	foreach ($header_text as $header) {
		if (preg_match('/^(?:Content-Type|Content-Language|Set-Cookie):/i', $header)) {
			header($header);
		}
	}
    //$n = strpos($contents,"\r\n\r\n");
    //$contents = substr($contents,$n+4);
    echo $contents;
}
$url = substr($_SERVER["PATH_INFO"],1);
if(empty($url)){
    $url = substr($_SERVER["REQUEST_URI"],11);
}
if($url){
    if(substr($url,0,6)=="http:/"&&$url[6]!='/'){
        $url="http://".substr($url,6);
    }
    if(!empty($_SERVER["QUERY_STRING"])&&!strpos($url,'?')){
        $url.="?".$_SERVER["QUERY_STRING"];
    }
    proxy($url);
}

